package comment

import (
	"center-platform/internal/controller/response"
	"center-platform/internal/controller/v1/comment/entity"
	"center-platform/internal/controller/v1/comment/service"
	"github.com/gin-gonic/gin"
	"reflect"
)

func List(c *gin.Context) {
	var commentSer service.CommentImpl = new(service.CommentService)
	var searchParam entity.SearchParam
	err := c.ShouldBindJSON(&searchParam)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	err, list := commentSer.List(&searchParam)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	response.ApiSuccess(c, list)
}

func Add(c *gin.Context) {
	var commentSer service.CommentImpl = new(service.CommentService)
	var comment entity.Comment
	err := c.ShouldBindJSON(&comment)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	param, _ := c.Get("user")
	comment.UserID = int(reflect.ValueOf(param).FieldByName("UserId").Int())
	err = commentSer.Add(&comment)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	response.ApiSuccess(c, "添加成功")
}
